<?php
    if(empty($_POST["name"])){
        echo "<script>alert('用户名不能为空！')</script>";
        echo "<script>history.go(-1)</script>";
        die();
    }
    if(strlen($_POST["pass"])<6){
        echo "<script>alert('密码小于六位！')</script>";
        echo "<script>history.go(-1)</script>";
        die();
    }
    if($_POST["pass"]!==$_POST["qpass"]){
        echo "<script>alert('两次密码不一致！')</script>";
        echo "<script>history.go(-1)</script>";
        die();
    }
    
    $pass_hash=password_hash($_POST["pass"],PASSWORD_DEFAULT);
    $name=$_POST["name"];

    $mysqli=new mysqli("localhost","root","root","tb_user");
    if($mysqli->connection_errno){
        die("数据库连接错误！".$mysqli->connection_errno);
    }

    if(isset($_POST["reg"])){
        $sql="INSERT INTO tuser(name,pass_hash) VALUES('$name','$pass_hash')";
        $mysqli->query($sql);
        if($mysqli->affected_rows>0){
            echo "<script>alert('注册成功！')</script>";
            echo "<script>window.location.href='http://localhost/rhythmgame/login.html'</script>";
        }
    }
?>